# -*- coding:utf-8 -*-
"""
@Time : 2025/01/31 15:00 PM
@Author: xiaomi
@Des: mysql数据库
"""

from dotenv import load_dotenv
import os
from tortoise import Tortoise

# 加载 .env 文件
load_dotenv()


# -----------------------数据库配置-----------------------------------
DB_ORM_CONFIG = {
    "connections": {
        "base": {
            'engine': 'tortoise.backends.mysql',
            "credentials": {
                'host': os.getenv('BASE_HOST', '127.0.0.1'),
                'user': os.getenv('BASE_USER', 'your_usernae'),
                'password': os.getenv('BASE_PASSWORD', 'your_password'),
                'port': int(os.getenv('BASE_PORT', 3306)),
                'database': os.getenv('BASE_DB', 'your_dbname'),
            }
        },
        # "base": os.getenv("DATABASE_URL", "mysql://user:password@localhost:3306/mydatabase"),
        # "db2": {
        #     'engine': 'tortoise.backends.mysql',
        #     "credentials": {
        #         'host': os.getenv('DB2_HOST', '127.0.0.1'),
        #         'user': os.getenv('DB2_USER', 'root'),
        #         'password': os.getenv('DB2_PASSWORD', '123456'),
        #         'port': int(os.getenv('DB2_PORT', 3306)),
        #         'database': os.getenv('DB2_DB', 'db2'),
        #     }
        # },
    },
    "apps": {
        "base": {"models": ["models.base", "aerich.models"], "default_connection": "base"},
        # "db2": {"models": ["models.db2"], "default_connection": "db2"},
    },
    'use_tz': False,
    'timezone': 'Asia/Shanghai'
}

async def init_db():
    await Tortoise.init(config=DB_ORM_CONFIG)
    # await Tortoise.generate_schemas()

async def close_db():
    await Tortoise.close_connections()